﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace Twitter.Tests
{
    [TestFixture]
    public class MessageProcessorContextFixture
    {
        [Test]
        public void SendingMessageOf140CharsEndsUpAsSingleTwitter()
        {
            MemoryBotClient sender = new MemoryBotClient("sender");
            MemoryBotClient client = new MemoryBotClient("test");
            sender.SetBuddy(client);

            using (MessageProcessorContext context = new MessageProcessorContext(null))
            {
                context.AddOutgoingMessage("test", new String('a', 140));
                context.SendOutgoingMessagesMax140Char(sender);
            }
            Assert.AreEqual(1, client.Messages.messages.Count);
            Assert.AreEqual(new String('a', 140), client.Messages.GetAll().Last().Text);
        }

        [Test]
        public void SendingMessageOf141CharsEndsUpAsTwoTwitters()
        {
            MemoryBotClient sender = new MemoryBotClient("sender");
            MemoryBotClient client = new MemoryBotClient("test");
            sender.SetBuddy(client);

            using (MessageProcessorContext context = new MessageProcessorContext(null))
            {
                context.AddOutgoingMessage("test", new String('a', 141));
                context.SendOutgoingMessagesMax140Char(sender);
            }
            Assert.AreEqual(2, client.Messages.messages.Count);
            Assert.AreEqual(new String('a', 136) + " ...", client.Messages.GetAll().OrderBy(x => x.Text.Length).Last().Text);
            Assert.AreEqual(new String('a', 5), client.Messages.GetAll().OrderBy(x => x.Text.Length).First().Text);
        }

        [Test]
        public void SendingMessageOf276CharsEndsUpAsTwoTwitters()
        {
            MemoryBotClient sender = new MemoryBotClient("sender");
            MemoryBotClient client = new MemoryBotClient("test");
            sender.SetBuddy(client);

            using (MessageProcessorContext context = new MessageProcessorContext(null))
            {
                context.AddOutgoingMessage("test", new String('a', 276));
                context.SendOutgoingMessagesMax140Char(sender);
            }
            Assert.AreEqual(2, client.Messages.messages.Count);
            Assert.AreEqual(new String('a', 136) + " ...", client.Messages.GetAll().OrderBy(x => x.Text).First().Text);
            Assert.AreEqual(new String('a', 140), client.Messages.GetAll().OrderBy(x => x.Text).Last().Text);
        }
    }
}
